const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disable: true,
    presub: true,
    phone: '',
    password: '',
    auth_code: '',
    text: '发送验证码',
    currenTime: 60,
    nickName: '',
    avatarUrl: '',
    unionid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    wx.getStorage({
      key: 'share_key',
      success: (res) => {
        self.setData({
          share_key: res.data,
        })
      },
      fail: function () {
        self.setData({
          share_key: '',
        })
      }
    })
    self.setData({
      nickName: decodeURI(options.nickName),
      avatarUrl: decodeURI(options.avatarUrl),
      unionid: decodeURI(options.unionid),
    })
  },
  // 获取随机数
  randomNum: function (minNum, maxNum) {
    switch (arguments.length) {
      case 1:
        return parseInt(Math.random() * minNum + 1, 10);
        break;
      case 2:
        return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
        break;
      default:
        return 0;
        break;
    }
  },
  register: function () {
    var self = this;
    wx.request({
      url: app.globalData.ApiUrl + "/index.php?act=connect&op=check_captcha",
      data: {
        auth_code: this.data.auth_code,
        phone: this.data.phone
      },
      method: 'GET',
      success: (res) => {
        if (res.data.code == 200) {

          wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=connect&op=check_mobile_exit',
            data: {
              client: 'wechat',
              type: 'wx',
              member_unionid: self.data.unionid,
              member_info: self.data.nickName,
              username: self.data.nickName,
              phone: self.data.phone,
              share_key: self.data.share_key,
              avatarUrl: self.data.avatarUrl
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            success: (res) => {
              if (res.data.code == 200) {
                if (res.data.datas.key) {
                  wx.setStorage({
                    key: 'key',
                    data: res.data.datas.key
                  })
                  wx.setStorage({
                    key: 'username',
                    data: res.data.datas.username
                  })
                  setTimeout(function () {
                    wx.switchTab({
                      url: '../member/member'
                    })
                  }, 200)
                  wx.showLoading({
                    title: '登录中',
                  })
                } else {
                  var params = "?member_unionid=" + self.data.unionid;
                  params += "&member_info=" + self.data.nickName;
                  params += "&username=" + self.data.nickName;
                  params += "&phone=" + self.data.phone;
                  params += "&share_key=" + self.data.share_key;
                  params += "&avatarUrl=" + self.data.avatarUrl;
                  wx.redirectTo({
                    url: '../member_set_password/member_set_password' + params
                  })
                }
              } else {
                wx.showToast({
                  title: res.data.datas.error,
                  icon: 'none'
                })
              }

            }
          });
        } else {
          wx.showToast({
            title: res.data.datas.error,
            icon: 'none'
          })
        }
      }
    })
  },
  password: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  phone: function (e) {
    this.setData({
      phone: e.detail.value
    })
    if (this.data.phone.trim().length == 11) {
      this.setData({
        disable: false,
        color: '#D50012'
      })
    } else {
      this.setData({
        disable: true
      })
    }
  },
  authCode: function (e) {
    this.setData({
      auth_code: e.detail.value
    })
    if (this.data.auth_code.trim().length == 6) {
      this.setData({
        presub: false
      })
    } else {
      this.setData({
        presub: true
      })
    }
  },
  sendCode: function () {
    var self = this;
    if (!/^1[3|4|5|6|7|8|9]\d{9}$/.test(this.data.phone)) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
    } else {
      wx.request({
        url: app.globalData.ApiUrl + '/index.php?act=connect&op=get_captcha&nchash=1&type=1&api=1',
        data: {
          phone: this.data.phone,
          client: 'wechat'
        },
        method: 'GET',
        success: (res) => {
          if (res.data.code == 200) {
            wx.showToast({
              title: '短信验证码已发出',
              icon: 'success'
            })
            self.setData({
              disable: true,
              color: '#ccc',
              text: self.data.currenTime + 's后请重试'
            })
            var interval = setInterval(function () {
              self.data.currenTime--;
              self.setData({
                text: self.data.currenTime + 's后请重试'
              })
              if (self.data.currenTime < 0) {
                clearInterval(interval);
                self.setData({
                  text: '重新发送',
                  disable: false,
                  color: '#D50012',
                  currenTime: 60
                })
              }
            }, 1000)
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none'
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.hideLoading()
  }
})